package com.ccl.iot.controler;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;

import com.ccl.iot.IOTInterface.IOTAuthMonitorInterface.IOTAuthChangeListener;
import com.ccl.iot.IOTInterface.IOTIconMonitorInterface.IOTIconChangeListener;
import com.ccl.iot.IOTInterface.IOTNameMonitorInterface.IOTNameChangeListener;
import com.ccl.iot.IOTInterface.IOTOnlineMonitorInterface;
import com.ccl.iot.IOTNetAPI;
import com.ccl.iot.IOTNetAPIEnum;
import com.ccl.iot.R;
import com.ccl.iot.device.IOTDevice;
import com.ccl.iot.publics.Messager;
import com.ccl.iot.publics.UIUtils;
import com.ccl.iot.views.ItemView;


public abstract class ControlerView extends ItemView<IOTDevice> implements IOTNameChangeListener, IOTOnlineMonitorInterface.IOTOnlineStatusChangeListener, IOTAuthChangeListener, IOTIconChangeListener {
    private boolean iDeviceBinding = false;

    public ControlerView(Context context, IOTDevice aDevice) {
        super(context, aDevice);
    }

    @Override
    public void setItem(IOTDevice aDevice) {
        if (aDevice != iItem) {
            boolean tBind = isDeviceBind();

            if (tBind) {
                unBindDevice();
            }

            iItem = aDevice;

            if (tBind) {
                bindDevice();
            }

            this.postInvalidate();
        }
    }

    public boolean isDeviceBind() {
        return iDeviceBinding;
    }

    public void bindDevice() {
        if (iDeviceBinding == false) {
            if (iItem != null) {
                iItem.addNameChangeListener(this);
                iItem.addIconChangeListener(this);
                iItem.addOnlineChangeListener(this);
                iDeviceBinding = iItem.addAuthChangeListener(this);
            }
        }
    }

    public void unBindDevice() {
        if (iDeviceBinding) {
            if (iItem != null) {
                iItem.addNameChangeListener(this);
                iItem.removeOnlineChangeListener(this);
                iDeviceBinding = (iItem.addAuthChangeListener(this) == false);
            }
        }
    }

    @Override
    public void onNameChanged(IOTDevice aSource, String aNewName) {
        ControlerView.this.postInvalidate();
    }

    @Override
    public void onIconChanged(IOTDevice aSource, String aNewName) {
        ControlerView.this.postInvalidate();
    }

    @Override
    public void onOnlineStatusChanged(IOTDevice aSource, boolean aOnline) {
        ControlerView.this.postInvalidate();
    }

    @Override
    public void onAuthChanged(IOTDevice aSource, int aNewAuth) {
        ControlerView.this.postInvalidate();
    }

    protected boolean checkAuth(final IOTDevice dev) {
        if (IOTNetAPIEnum.EAUTH_STATE.IS_REQUESTABLE(dev.getAuth())) {
            final AlertDialog.Builder build = new AlertDialog.Builder(getContext(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
            build.setMessage(R.string.Is_Request_Auth);
            build.setPositiveButton(R.string.OK, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    IOTNetAPI iotNetAPI = IOTNetAPI.GetInstance();
                    if (iotNetAPI != null) {
                        boolean result = iotNetAPI.authRequest(dev.getID(), false);
                        if (result) {
                            Messager.Show(UIUtils.getContext().getResources().getString(R.string.AuthRequestSent), 0);
                        }
                    }
                }
            });

            build.setNegativeButton(R.string.Cancel, null);
            UIUtils.postTaskSafely(new Runnable() {
                @Override
                public void run() {
                    build.show();
                }
            });

            return false;
        }

        return true;
    }

}
